package com.tycmc.signelectroniccontracts.tools.permissions;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.tycmc.signelectroniccontracts.tools.base.ActBase;

import java.util.ArrayList;
import java.util.List;


/**
 * 权限判断
 */

public class PermissionsTool {


    /**
     * 调用基本权限
     *
     * @param permission    权限
     * @param missInterface 权限是否通过回调
     * @param base          基类
     */
    public static void requestPermiss(final String[] permission, final PerMissionCallBack missInterface, ActBase base) {

        base.setOnPermissionsResultCallBack(new ActBase.OnRequestPermissionsResultCallBack() {
            @Override
            public void onRequestPermissionResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
                int x = 0;
                List<String> list = new ArrayList<String>();
                for (int i = 0; i < grantResults.length; i++) {
                    x = x + grantResults[i];
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        try {
                            list.add(permissions[i]);
                        } catch (Exception e) {
                        }
                    }
                }
                if (missInterface != null) {

                    if (x == PackageManager.PERMISSION_GRANTED) {
                        missInterface.permissIsPass(permissions, true);
                    } else {
                        String[] strings = new String[list.size()];
                        for (int i = 0; i < list.size(); i++) {
                            strings[i] = list.get(i);
                        }
                        missInterface.permissIsPass(strings, false);
                    }
                }
            }
        });

        if (permission == null || permission.length == 0) {//权限为空，则不通过
            missInterface.permissIsPass(null, false);
        } else {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                boolean isPass = false;
                for (int i = 0; i < permission.length; i++) {
                    if (ActivityCompat.checkSelfPermission(base, permission[i]) != PackageManager.PERMISSION_GRANTED) {
                        isPass = true;
                        break;
                    }
                }
                if (isPass) {
                    ActivityCompat.requestPermissions(base, permission, 1);
                } else {
                    missInterface.permissIsPass(permission, true);
                }
            } else {
                missInterface.permissIsPass(permission, true);
            }
        }
    }

    /**
     * 安装apk时需要申请安装权限
     * @param actBase
     * @param perMissInterface
     */
    public static void requestPermiss(ActBase actBase, final PerMissionCallBack perMissInterface) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            boolean b = actBase.getPackageManager().canRequestPackageInstalls();
            if(b){
                perMissInterface.permissIsPass(null,true);
            }else {
                Uri packageURI = Uri.parse("package:" + actBase.getPackageName());
                Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES,packageURI);
                actBase.startActivityForResult(intent, 123);
                actBase.setOnActivityResultCallBack(new ActBase.OnActivityResultCallBack() {
                    @Override
                    public void onResult(int requestCode, int resultCode, Intent data) {
                        if (requestCode == 123) {
                            perMissInterface.permissIsPass(null, true);
                        }
                    }

                });
            }
        }else {
            perMissInterface.permissIsPass(null,true);
        }


    }


    public static void jiaobiaorequestPermiss(ActBase actBase, final PerMissionCallBack perMissInterface) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            boolean b = actBase.getPackageManager().canRequestPackageInstalls();
            if(b){
                perMissInterface.permissIsPass(null,true);
            }else {
                Uri packageURI = Uri.parse("package:" + actBase.getPackageName());
                Intent intent = new Intent("com.huawei.android.launcher.permission.WRITE_SETTINGS",packageURI);
                actBase.startActivityForResult(intent, 124);
                actBase.setOnActivityResultCallBack(new ActBase.OnActivityResultCallBack() {
                    @Override
                    public void onResult(int requestCode, int resultCode, Intent data) {
                        if (requestCode == 124) {
                            perMissInterface.permissIsPass(null, true);
                        }
                    }

                });
            }
        }else {
            perMissInterface.permissIsPass(null,true);
        }


    }


    public interface PerMissionCallBack {
        void permissIsPass(String[] permission, boolean isPass);
    }
}
